package com.gudaomai.wuye.web;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.gudaomai.wuye.common.CommonConstant;
import com.gudaomai.wuye.common.CommonException;
import com.gudaomai.wuye.common.MsgConstant;
import com.gudaomai.wuye.entity.House;
import com.gudaomai.wuye.entity.HouseRental;
import com.gudaomai.wuye.entity.HouseRentalInput;
import com.gudaomai.wuye.entity.RetEntity;
import com.gudaomai.wuye.service.IHouseRentalServ;
import com.gudaomai.wuye.web.BaseController;

import io.swagger.annotations.ApiOperation;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zenghualu
 * @since 2017-08-18
 */
@Controller
@RequestMapping("/wuye/houseRental")
public class HouseRentalCtrl extends BaseController {
	
    @Autowired 
	IHouseRentalServ  houseRentalService;
	
	/** 
	 * add a new houseRental information
	 * 
	 * 1. Check if all required field are filled in.
	 * 2. Check if all the inputs are valid.
	 * 3. Check if houseRental object with the same properties does not exist in the DB.
	 * 4. Save the inputed values.
	 * 
	 *@param  
	 *@author jess
	 *return 
	 *
	 **/
    @ApiOperation("The house rent info settings page, click the add button, add house rent details")
	@RequestMapping(value = "/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public RetEntity addHouseRental(@RequestBody HouseRentalInput houseRentalInput) throws Exception {
		
		RetEntity entity = new RetEntity();
		
		if(houseRentalService.isExist(houseRentalInput.getIdHouse())){
			// The house Rent info already exists in the database
			
		throw new CommonException(CommonConstant.RESULT_CODE_CREATED, 
		getMessage(MsgConstant.RET_HOUSERENTAL_ADD_FAILURE));
		} 
		else{
			//Create new house rental entity
			HouseRental houseRentalEntity = new HouseRental();
			
			// copy all user inputs to the new house entity
			BeanUtils.copyProperties(houseRentalInput, houseRentalEntity);
				
			// Insert the house entity into the database
			houseRentalService.insert(houseRentalEntity);
						
			//Return message and HTTP result code
			entity.setRetCode(CommonConstant.RESULT_CODE_OK);
			entity.setRetMessage(getMessage(MsgConstant.RET_HOUSERENTAL_ADD_SUCCESS));
		}
		
		return entity;
	}
	
	/** 
	 * query houseRental information
	 * 1. Check if the corresponding Id exists in the DB.
	 * 2. Read and return the result set from DB .
	 * 
	 *@param  id
	 *@author jess
	 *return 
	 *
	 **/
    
    @ApiOperation("The house rent info settings page, click the query button, query house using house's id")	
	@GetMapping("/id/{id}")
	public HouseRental getHouseRentalById(@PathVariable("id") String id) throws Exception{
        
    	HouseRental houseRental = houseRentalService.selectById(id);
		
    	houseRental.setRetCode(CommonConstant.RESULT_CODE_OK);
    	houseRental.setRetMessage(getMessage(MsgConstant.RET_HOUSERENTAL_SELECT_SUCCESS));
		return houseRental;
	}
	
	/** 
	 * update the existing houseRental information.
	 * 
	 * 1. Check if all required field are filled in.
	 * 2. Check if all the inputs are valid.
	 * 3. update HouseRental information in the DB.
	 * 
	 *@param 
	 *@author jess
	 *return 
	 *
	 **/
    @ApiOperation("The house rent info settings page, click the modify button, update HouseRental")
	@RequestMapping(value = "/", method = RequestMethod.PUT)
	public RetEntity updateHouseRental(@RequestBody HouseRentalInput houseRentalInput) throws Exception{
		
        RetEntity entity = new RetEntity();
		
		// update the houseRental entity in the database
		// Create new houseRental entity
		HouseRental houseRentalEntity = new HouseRental();
		// copy all user inputs to the new commRegion entity
		BeanUtils.copyProperties(houseRentalInput, houseRentalEntity);
		
		houseRentalService.updateById(houseRentalEntity);
		
		//Return message and HTTP result code
		entity.setRetCode(CommonConstant.RESULT_CODE_OK);
		entity.setRetMessage(getMessage(MsgConstant.RET_HOUSERENTAL_UPDATE_SUCCESS));
		
		return entity;
	}
	
	
	/** 
	 * delete the existing houseRental information
	 * 1. Check if the corresponding Id exists in the DB.
	 * 2. Delete the houseRental information from the DB.
	 * 
	 *@param  id
	 *@author jess
	 *return 
	 *
	 **/
    @ApiOperation("The House rent info settings page, click the delete button, delete HouseRental")
	@RequestMapping(value="/id/{id}",method=RequestMethod.DELETE)
	public RetEntity delHouseRentalById(@PathVariable("id") String id) throws Exception{
		

        RetEntity entity = new RetEntity();
		
		houseRentalService.deleteById(id);
		
		entity.setRetCode(CommonConstant.RESULT_CODE_OK);
		entity.setRetMessage(getMessage(MsgConstant.RET_HOUSERENTAL_DELETE_SUCCESS));
		return entity;
	}
}
